/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.proyecto.datos.EntityAlumno;
import com.proyecto.datos.EntityCarrera;
import com.proyecto.modelado.Alumno;
import com.proyecto.modelado.Carrera;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author mario
 */
public class frmCrearAlumno extends javax.swing.JInternalFrame {

    EntityCarrera entCarre;
    ArrayList arry;
    Carrera carre;
    Alumno al;
    EntityAlumno entAl;
    int id_carrera;
    
    
    public void Limpiarfrm(){
        this.jtxtCarnet2.setText("");
        this.jtxtNombres2.setText("");
        this.jtxtApellidos2.setText("");
    }
    
    public void CargarComboBox(){
        
        entCarre = null;
        entAl = null;
        al = new Alumno();
        carre = new Carrera();
        arry = new ArrayList();
        
        //limpiar el combo box
        
         try {
            JList list = new JList();
            DefaultListModel list_model = new DefaultListModel();
            arry = carre.MostrarCarrera();
            list_model.addElement("Seleccione");
            if (arry != null) {
                for (int i = 0; i < arry.size(); i++) {
                    entCarre = (EntityCarrera) arry.get(i);
                    list_model.addElement(entCarre.getNombre_carrera());

                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Carrera", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }

            list.setModel(list_model);
            this.jcbCarrera2.setModel(new DefaultComboBoxModel(list_model.toArray()));
            this.jcbCarrera2.setSelectedIndex(0);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
        }
    }

    /**
     * Creates new form frmCrearAlumno
     */
    public frmCrearAlumno() {
        initComponents();


        this.setTitle("Crear Nuevo Alumno");

        

        CargarComboBox();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlblCarnet2 = new javax.swing.JLabel();
        jlblAgregarAlumno2 = new javax.swing.JLabel();
        jtxtCarnet2 = new javax.swing.JTextField();
        jtxtNombres2 = new javax.swing.JTextField();
        jtxtApellidos2 = new javax.swing.JTextField();
        jlblApellidos2 = new javax.swing.JLabel();
        jlblNombres2 = new javax.swing.JLabel();
        jlblCarrera2 = new javax.swing.JLabel();
        jcbCarrera2 = new javax.swing.JComboBox();
        jbtnAgregar2 = new javax.swing.JButton();
        jbtnCancelar2 = new javax.swing.JButton();

        setClosable(true);

        jlblCarnet2.setText("Carnet:");

        jlblAgregarAlumno2.setText("Agregar Alumno");

        jtxtApellidos2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtApellidos2ActionPerformed(evt);
            }
        });

        jlblApellidos2.setText("Apellidos:");

        jlblNombres2.setText("Nombres:");

        jlblCarrera2.setText("Carrera:");

        jcbCarrera2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbCarrera2ItemStateChanged(evt);
            }
        });

        jbtnAgregar2.setText("Agregar");
        jbtnAgregar2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnAgregar2MouseClicked(evt);
            }
        });
        jbtnAgregar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAgregar2ActionPerformed(evt);
            }
        });

        jbtnCancelar2.setBackground(new java.awt.Color(202, 82, 82));
        jbtnCancelar2.setText("Cancelar");
        jbtnCancelar2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCancelar2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblCarrera2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlblCarnet2)
                            .addComponent(jlblNombres2)
                            .addComponent(jlblApellidos2))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtxtCarnet2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtNombres2, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(jtxtApellidos2, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(jcbCarrera2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(182, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbtnAgregar2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnCancelar2)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jlblAgregarAlumno2)
                        .addGap(149, 149, 149))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jlblAgregarAlumno2)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblCarnet2)
                    .addComponent(jtxtCarnet2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblNombres2)
                    .addComponent(jtxtNombres2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtApellidos2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblApellidos2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbCarrera2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblCarrera2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAgregar2)
                    .addComponent(jbtnCancelar2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtxtApellidos2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtApellidos2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtApellidos2ActionPerformed

    private void jbtnAgregar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAgregar2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnAgregar2ActionPerformed

    private void jbtnCancelar2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCancelar2MouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtnCancelar2MouseClicked

    private void jcbCarrera2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbCarrera2ItemStateChanged
        // TODO add your handling code here:
        for (int i = 0; i < arry.size(); i++) {
            entCarre = (EntityCarrera) arry.get(i);
            if (this.jcbCarrera2.getSelectedItem().equals(entCarre.getNombre_carrera())) {
                this.id_carrera = entCarre.getId_carrera();
            }
        }
    }//GEN-LAST:event_jcbCarrera2ItemStateChanged

    private void jbtnAgregar2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnAgregar2MouseClicked
        // TODO add your handling code here:
        String carnet = this.jtxtCarnet2.getText();
        String nombre = this.jtxtNombres2.getText();
        String apellido= this.jtxtApellidos2.getText();
        int id_carrera = this.id_carrera;
        String msj = al.InsertarAlumo(nombre, apellido, carnet, id_carrera);
        JOptionPane.showMessageDialog(rootPane, msj, "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        CargarComboBox();
        Limpiarfrm();
        
    }//GEN-LAST:event_jbtnAgregar2MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtnAgregar2;
    private javax.swing.JButton jbtnCancelar2;
    private javax.swing.JComboBox jcbCarrera2;
    private javax.swing.JLabel jlblAgregarAlumno2;
    private javax.swing.JLabel jlblApellidos2;
    private javax.swing.JLabel jlblCarnet2;
    private javax.swing.JLabel jlblCarrera2;
    private javax.swing.JLabel jlblNombres2;
    private javax.swing.JTextField jtxtApellidos2;
    private javax.swing.JTextField jtxtCarnet2;
    private javax.swing.JTextField jtxtNombres2;
    // End of variables declaration//GEN-END:variables
}
